#!/usr/bin/env python3
#-*-encoding:utf-8-*-

import os
from collections import deque 

#清空控制台
def clear():
    print("\033c",end="")


def find_lowest_cost_node(costs):
    lowest_cost = float("inf")
    lowest_cost_node = None
    for node in costs:
        cost = costs[node]
        if cost < lowest_cost and node not in processed:
            lowest_cost = cost
            lowest_cost_node = node
    return lowest_cost_node

clear()  #清屏
graph["start"] = {}
graph["start"]["a"] = 6
graph["start"]["b"] = 2
graph["a"] = {}
graph["a"]["fin"] = 1

graph["b"] = {}
graph["b"]["a"] = 3
graph["b"]["fin"] = 5

graph["fin"] = {}

#创建开销表
infinity = float("inf")
costs = { }
costs["a"] = 6
costs["b"] = 2
costs["fin"] = infinity

#存储父节点散列表
parents = {}
parents["a"] = "start"
parents["b"] = "start"
parents["fin"] = None

processed = []
node = find_lowest_cost_node(costs) #找到最小开销节点
while node is not None:
    cost = costs[node]
    neightbors = graph[nopde]
    for n in neightbors.keys(): #遍历当前节点的所有邻居
        new_cost = cost + neightbors[n]
        if costs[n] > new_cost:
            costs[n] = new_cost #更新开销
            parents[n] = node 
    processed.append(node)
    node = find_lowest_cost_node(costs)
